package com.category.douyijia.services;

import java.time.LocalDateTime;
import java.util.Date;

import com.category.douyijia.base.BaseService;
import com.category.douyijia.dao.SessionDao;
import com.category.douyijia.dao.UserDao;
import com.category.douyijia.model.Session;
import com.category.douyijia.model.User;
import com.category.douyijia.model.UserLoginState;
import com.jfinal.aop.Aop;
import com.jfinal.aop.Inject;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.kit.TimeKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class LoginService extends BaseService<LoginService, User> {
	
	public static final String LOGIN_USER = "login_user";
	public static final String SESSION_ID = "sessionId";
	@Inject
	private User user;
	private SessionDao sessionDao = new SessionDao();
	private UserLoginState userLoginStateDao = new UserLoginState().dao();
	private UserDao userDao = Aop.get(UserDao.class);
	public Ret userLogin(String userId, String password
						, String ip, int port){
		if (StrKit.isBlank(userId)) {
			return Ret.fail("msg", "账号不能为空");
		}
		if (StrKit.isBlank(password)) {
			return Ret.fail("msg", "密码不能为空");
		}
		
		return null;
	}
	private Ret doLogin(String userId,String password){
		userId = userId.toLowerCase().trim();
		password = password.trim();
		user = userDao.selectUserByToken(userId, password);
		if (user == null) {
			return Ret.fail("msg", "用户名或密码不正确！");
		}
		if (user.getUserStatue()<1) {
			return Ret.fail("msg", "账号未激活");
		}
		
		String name = user.getUserName();
		//使用jfinal加密机制进行加密
		String hashPwd = HashKit.sha256(name+password);
		if (!user.getUserPassword().equals(hashPwd)) {
			return Ret.fail("msg", "账号或密码不正确");
		}
		//设置有效登录时间为5天
		long timeToLiveSeconds = 5*24*60*60;
		//设置失效时间
		Date overTime = TimeKit.toDate(
									LocalDateTime.now()
									.plusSeconds(timeToLiveSeconds)
									);
		Session session = new Session();
		String sessionId = StrKit.getRandomUUID();
		//设置SessionID
		session.setId(sessionId);
		//设置用户登录ID
		session.setAccountId(user.getUserId());
		//设置登录时间
		session.setCreated(new Date());
		//设置保存时长
		session.setExpires(overTime);
		session.save();
		//隐藏重要信息
		user.hiddenKey();
		
		return Ret.ok(SESSION_ID, sessionId)
					.set(LOGIN_USER,userId)
					.set("overTime", overTime);
	}
	private void createLoginLog(Ret loginRet, String userId){
		Record loginLog = new Record();
		UserLoginState userLoginState = null;
		if (loginRet.isOk()) {
			userLoginState = userLoginStateDao.findFirst("select * from"
					+ "t_user_login_state where user_login_id = ?", userId);
			User loginUser = loginRet.getAs(LOGIN_USER);
			loginUser.loginIt(userId, 1,userLoginState);
			loginLog.setColumns(user);
		}
		
	}
	/**
	 * 退出登录机制
	 * @param userId
	 * 用户ID
	 * @return
	 * 返回Ret对象
	 */
	public Ret logout(String userId){
		boolean result = false;
		result = sessionDao.deleteSessionById(userId);
		if (result) {
			return Ret.ok();
		}else {
			return Ret.fail();
		}
	}
}
